<?php
namespace app\admin\controller;
use think\Db;
/**
 * 用户管理
 */
class MemberController extends BaseController {
    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $ziduan = 'user_id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] =array("like","%".$keyword."%");
        }
		$this->assign('ziduan', $ziduan);
        $member_list = db('member')->where($map)->order($order)->paginate(15, false, get_query());
        return $this->fetch('index', ['member_list' => $member_list, 'keyword' => $keyword]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }
	/**
	 * 用户收货地址
	 */
	public function address(){
		$where['id']=['neq',0];
		if(input("keyword")){
			$where['consignee|mobile']=['like','%'.input("keyword").'%'];
		}
		$list=db("member_address")->where($where)->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
    /**
     * 保存用户
     */
    public function save() {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Member');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
            	$password = $data['password'];
                $data['password'] = md5_salt($password);
                $data['pay_password'] = md5_salt($password);
                if ($this->member_model->allowField(true)->save($data)) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id) {
    	 if ($this->request->isPost()) {
            $data['user_name']=input("user_name");
			$data['status']=input("status");
            if (!empty($data['password'])) {
                $data['password'] = md5_salt($data['password']);
            }
            if (db("member")->where("user_id",1)->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }

        }
        $member =db("member")->where("user_id",input("id"))->find();
        return $this->fetch('edit', ['member' => $member]);
    }
    
    
}
